from header_common import *
from header_operations import *
from header_parties import *
from header_items import *
from header_skills import *
from header_triggers import *
from header_troops import *

from module_constants import *

####################################################################################################################
#  Each trigger contains the following fields:
# 1) Check interval: How frequently this trigger will be checked
# 2) Delay interval: Time to wait before applying the consequences of the trigger
#    After its conditions have been evaluated as true.
# 3) Re-arm interval. How much time must pass after applying the consequences of the trigger for the trigger to become active again.
#    You can put the constant ti_once here to make sure that the trigger never becomes active again after it fires once.
# 4) Conditions block (list). This must be a valid operation block. See header_operations.py for reference.
#    Every time the trigger is checked, the conditions block will be executed.
#    If the conditions block returns true, the consequences block will be executed.
#    If the conditions block is empty, it is assumed that it always evaluates to true.
# 5) Consequences block (list). This must be a valid operation block. See header_operations.py for reference. 
####################################################################################################################

# Some constants for use below
merchant_inventory_space = 30
num_merchandise_goods = 36



triggers = [
# Tutorial:
  (0.1, 0, ti_once, [(map_free,0)], [(tutorial_box,"str_tutorial_map1")]),

# Refresh Merchants
  (0.0, 0, 24.0, [], [
                      (reset_item_probabilities,100),
                      (set_merchandise_modifier_quality,150),


                      (reset_item_probabilities,100),(set_item_probability_in_merchandise,"itm_tools",700),
                      (troop_add_merchandise,"trp_zendar_merchant",itp_type_goods,num_merchandise_goods),
                      (reset_item_probabilities,100),(set_item_probability_in_merchandise,"itm_salt",700),
                      (troop_add_merchandise,"trp_salt_mine_merchant",itp_type_goods,num_merchandise_goods),

                      # Add trade goods to merchant inventories
#                      (store_sub, ":item_to_production_slot", slot_town_trade_good_productions_begin, trade_goods_begin),
                      (store_sub, ":item_to_price_slot", slot_town_trade_good_prices_begin, trade_goods_begin),

                      (try_for_range,":cur_center",towns_begin,towns_end),
                        (party_get_slot,":cur_merchant",":cur_center",slot_town_merchant),
                        (reset_item_probabilities,100),
                        (try_for_range, ":cur_goods", trade_goods_begin, trade_goods_end),
#                          (store_add, ":cur_production_slot", ":cur_goods", ":item_to_production_slot"),
                          (store_add, ":cur_price_slot", ":cur_goods", ":item_to_price_slot"),
#                          (party_get_slot, ":cur_production", ":cur_center", ":cur_production_slot"),
                          (party_get_slot, ":cur_price", ":cur_center", ":cur_price_slot"),
                      
##                          (assign, ":cur_probability", 100),
##                          (store_add, ":cur_production", 100, ":cur_production"),
##                          (val_mul, ":cur_probability", ":cur_production"),
##                          (val_div, ":cur_probability", 100),
##                          (try_begin),
##                            (gt, ":cur_probability", 100),
##                            (store_sub, ":temp_dif", ":cur_probability", 100),
##                            (val_mul, ":temp_dif", 4),
##                            (val_add, ":cur_probability", ":temp_dif"),
##                          (try_end),
##                          (store_sub, ":temp_dif", average_price_factor, ":cur_price"),
##                          (val_div, ":temp_dif", 2),
##                          (val_add, ":cur_price", ":temp_dif"),
                          (assign, ":cur_probability", 100),
                          (val_mul, ":cur_probability", average_price_factor),
                          (val_div, ":cur_probability", ":cur_price"),
                          (val_mul, ":cur_probability", average_price_factor),
                          (val_div, ":cur_probability", ":cur_price"),
                          (val_mul, ":cur_probability", average_price_factor),
                          (val_div, ":cur_probability", ":cur_price"),
                          (val_mul, ":cur_probability", average_price_factor),
                          (val_div, ":cur_probability", ":cur_price"),
                          (set_item_probability_in_merchandise,":cur_goods",":cur_probability"),
                        (try_end),
                        (troop_add_merchandise,":cur_merchant",itp_type_goods,num_merchandise_goods),

                        (troop_ensure_inventory_space,":cur_merchant",merchant_inventory_space),
                        (store_troop_gold, ":cur_gold",":cur_merchant"),
                        (lt,":cur_gold",1000),
                        (store_random_in_range,":new_gold",100,200),
                        (troop_add_gold,":cur_merchant",":new_gold"),
                      (try_end),

                      #Adjusting salt mine merchant
                      (reset_item_probabilities, 0),
                      (set_item_probability_in_merchandise, "itm_salt", 100),
                      (troop_add_merchandise,"trp_salt_mine_merchant",itp_type_goods,4),
                      (troop_ensure_inventory_space,"trp_salt_mine_merchant",merchant_inventory_space),
                      (store_troop_gold, ":cur_gold","trp_salt_mine_merchant"),
                      (lt,":cur_gold",400),
                      (store_random,":new_gold",50),
                      (val_add,":new_gold",100),
                      (troop_add_gold,"trp_salt_mine_merchant",":new_gold"),
                      (try_for_range, ":cur_good", trade_goods_begin, trade_goods_end),
                        (store_sub, ":cur_good_price_slot", ":cur_good", trade_goods_begin),
                        (val_add, ":cur_good_price_slot", slot_town_trade_good_prices_begin),
                        (try_begin),
                          (eq, ":cur_good", "itm_salt"),
                          (party_set_slot, "p_salt_mine", ":cur_good_price_slot", 100),
                        (else_try),
                          (party_set_slot, "p_salt_mine", ":cur_good_price_slot", 50),
                        (try_end),
                      (try_end),
                     ]),

# Refresh Armor sellers
  (0.0, 0, 24.0, [], [
                      (reset_item_probabilities,100),
                      (set_merchandise_modifier_quality,150),
                      (try_for_range,reg(2),armor_merchants_begin,armor_merchants_end),
                        (troop_add_merchandise,reg(2),itp_type_body_armor,16),
                        (troop_add_merchandise,reg(2),itp_type_head_armor,16),
                        (troop_add_merchandise,reg(2),itp_type_foot_armor,8),
                        (troop_add_merchandise,reg(2),itp_type_hand_armor,4),
                        (troop_ensure_inventory_space,reg(2),merchant_inventory_space),
                        (store_troop_gold, reg(6),reg(2)),
                        (lt,reg(6),400),
                        (store_random_in_range,":new_gold",100,200),
                        (troop_add_gold,reg(2),":new_gold"),
                      (end_try,0),
                     ]),


# Refresh Weapon sellers
  (0.0, 0, 24.0, [], [
                      (reset_item_probabilities,100),
                      (set_merchandise_modifier_quality,150),
                      (try_for_range,reg(2),weapon_merchants_begin,weapon_merchants_end),
                        (troop_add_merchandise,reg(2),itp_type_one_handed_wpn,5),
                        (troop_add_merchandise,reg(2),itp_type_two_handed_wpn,5),
                        (troop_add_merchandise,reg(2),itp_type_polearm,5),
                        (troop_add_merchandise,reg(2),itp_type_shield,6),
                        (troop_add_merchandise,reg(2),itp_type_bow,4),
                        (troop_add_merchandise,reg(2),itp_type_crossbow,3),
                        (troop_add_merchandise,reg(2),itp_type_thrown,5),
                        (troop_add_merchandise,reg(2),itp_type_arrows,2),
                        (troop_add_merchandise,reg(2),itp_type_bolts,2),
                        (troop_ensure_inventory_space,reg(2),merchant_inventory_space),
                        (store_troop_gold, reg(6),reg(2)),
                        (lt,reg(6),400),
                        (store_random_in_range,":new_gold",100,200),
                      (troop_add_gold,reg(2),":new_gold"),
                      (end_try,0),
                     ]),

# Refresh Horse sellers
  (0.0, 0, 24.0, [], [
                      (reset_item_probabilities,100),
                      (set_merchandise_modifier_quality,150),
                      (try_for_range,":cur_merchant",horse_merchants_begin,horse_merchants_end),
                        (troop_add_merchandise,":cur_merchant",itp_type_horse,5),
                        (troop_ensure_inventory_space,":cur_merchant",65),
                        (store_troop_gold, ":cur_gold",":cur_merchant"),
                        (lt,":cur_gold",600),
                        (store_random_in_range,":new_gold",100,200),
                        (troop_add_gold,":cur_merchant",":new_gold"),
                      (try_end),
                     ]),
  
# Refresh Tavernkeepers
  (24.0, 0, 0.0, [], [
    # Refresh Zendar
                      (try_begin,0),
                      (assign,reg(2),"p_zendar_mercs"),
                      (store_party_size,reg(6),reg(2)),
                      (lt,reg(6),20),
                      (store_random,reg(7),4),
                      (val_add,reg(7),1),
                      (party_add_members,reg(2),"trp_townsman",reg(7)),
                      (store_random,reg(8),2),
                      (party_add_members,reg(2),"trp_watchman",reg(8)),
                      (end_try,0),
    # Refresh Swadians
                      (try_for_range,":town_no",towns_begin,towns_end),
                        (party_get_slot, ":merc_party", ":town_no", slot_town_mercs),
                        (gt, ":merc_party", 0),
                        (store_party_size,reg(6), ":merc_party"),
                        (lt,reg(6),20),
                        (store_random,reg(7),4),
                        (val_add,reg(7),1),
                        (store_faction_of_party, ":faction_no", ":town_no"),
                        (faction_get_slot, ":tier_1_troop", ":faction_no", slot_faction_tier_1_troop),
                        (faction_get_slot, ":tier_2_troop", ":faction_no", slot_faction_tier_2_troop),
                        (gt, ":tier_2_troop", 0),
                        (party_add_members, ":merc_party", ":tier_1_troop", reg(7)),
                        (store_random,reg(8),2),
                        (party_add_members, ":merc_party", ":tier_2_troop", reg(8)),
                      (end_try,0),
                     ]),


#############

#Captivity:

#  (1.0, 0, 0.0, [],
#   [
#       (ge,"$captivity_mode",1),
#       (store_current_hours,reg(1)),
#       (val_sub,reg(1),"$captivity_end_time"),
#       (ge,reg(1),0),
#       (display_message,"str_nobleman_reached_destination"),
#       (jump_to_menu,"$captivity_end_menu"),
#    ]),


  (1.0, 0, ti_once, [], [(assign,"$peak_river_pirates",6)]),
  (3.7, 0, 0.0, [#[store0_distance_to_town_from_party,zendar],[lt,reg(0),40],
                 (store_num_parties_of_template, reg(2), "pt_river_pirates"),
                 (lt,reg(2),"$peak_river_pirates")],
                    [(set_spawn_radius,1),(spawn_around_party,"p_river_pirate_spawn_point","pt_river_pirates")]),

  (5.7, 0, 0.0, [(store_num_parties_created,reg(3),"pt_manhunters"),
                 (lt,reg(3),num_max_zendar_manhunters),
                 (store_num_parties_of_template, reg(2), "pt_manhunters"), (lt,reg(2),3)],
                       [(set_spawn_radius,1),(spawn_around_party,"p_zendar","pt_manhunters")]),
#  (5.2, 0, 0.0, [(store_num_parties_created,reg(3),"pt_peasant"),(lt,reg(3),num_max_zendar_peasants),
#                 (store_num_parties_of_template, reg(2), "pt_peasant"), (lt,reg(2),3)],
#                          [(set_spawn_radius,1),(spawn_around_party,"p_zendar","pt_peasant")]),
  

  (0.0, 0, 0.0, [(neq,"$quest_accepted_zendar_river_pirates",0),(eq,"$quest_complete_zendar_river_pirates",0)],
   [
    (store_num_parties_destroyed_by_player, ":num_river_pirates_destroyed", "pt_river_pirates"),
    (assign, ":percentage", 100),
    (val_mul, ":percentage", ":num_river_pirates_destroyed"),
    (val_div, ":percentage", num_max_river_pirates),
    (set_quest_progression, "qst_hunt_down_river_pirates", ":percentage"),
   ]),
  
  (0.1, 0, ti_once, [(store_num_parties_destroyed_by_player, ":num_river_pirates_destroyed", "pt_river_pirates"),
                     (ge,":num_river_pirates_destroyed",num_max_river_pirates)],
                          [(assign,"$quest_complete_zendar_river_pirates",1),
                           (set_quest_progression, "qst_hunt_down_river_pirates", 100),
                           (assign,"$peak_river_pirates",2)]),
 

#Tax Collectors
# Prisoner Trains
#  (4.1, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_prisoner_train"),
#                         (assign, "$pin_limit", peak_prisoner_trains),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                         (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#                         (party_set_ai_object,"$pout_party","$pout_town"),
#                    ]),
#
#  (4.1, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_prisoner_train"),
#                         (assign, "$pin_limit", peak_prisoner_trains),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                         (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#                         (party_set_ai_object,"$pout_party","$pout_town"),
#                    ]),

  (2.0, 0, 0, [(store_random_party_of_template, reg(2), "pt_prisoner_train_party"),
               (party_is_in_any_town,reg(2)),
               ],
              [(store_faction_of_party, ":faction_no", reg(2)),
               (call_script,"script_cf_select_random_center_with_faction", ":faction_no"),
               (party_set_ai_behavior,reg(2),ai_bhvr_travel_to_party),
               (party_set_ai_object,reg(2),reg0),
               (party_set_flags, reg(2), pf_default_behavior, 0),
            ]),

##Caravans
#  (4.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_caravan"),
#                         (assign, "$pin_limit", peak_kingdom_caravans),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                         (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#                         (party_set_ai_object,"$pout_party","$pout_town"),
#                    ]),

#  (4.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_caravan"),
#                         (assign, "$pin_limit", peak_kingdom_caravans),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                         (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#                         (party_set_ai_object,"$pout_party","$pout_town"),
#                    ]),

##  (2.0, 0, 0, [(store_random_party_of_template, reg(2), "pt_kingdom_caravan_party"),
##               (party_is_in_any_town,reg(2)),
##               ],
##              [(store_faction_of_party, ":faction_no", reg(2)),
##               (call_script,"script_cf_select_random_town_with_faction", ":faction_no"),
##               (party_set_ai_behavior,reg(2),ai_bhvr_travel_to_party),
##               (party_set_ai_object,reg(2),reg0),
##               (party_set_flags, reg(2), pf_default_behavior, 0),
##            ]),
  
  (4.0, 0, 0.0, [(eq, "$caravan_escort_state", 1), #cancel caravan_escort_state if caravan leaves the destination
                 (get_party_ai_object,reg(1),"$caravan_escort_party_id"),
                 (neq,reg(1),"$caravan_escort_destination_town"),
                ],
                     [(assign,"$caravan_escort_state",0),
#                      (add_xp_as_reward,100),
                      ]),

#Messengers
#  (4.2, 0, 0.0, [],
#   [(assign, "$pin_faction", "fac_swadians"),
#    (assign, "$pin_party_template", "pt_swadian_messenger"),
#    (assign, "$pin_limit", peak_kingdom_messengers),
#    (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#    (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#    (party_set_ai_object,"$pout_party","$pout_town"),
#    ]),

#  (4.2, 0, 0.0, [],
#   [(assign, "$pin_faction", "fac_vaegirs"),
#    (assign, "$pin_party_template", "pt_vaegir_messenger"),
#    (assign, "$pin_limit", peak_kingdom_caravans),
#    (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#    (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#    (party_set_ai_object,"$pout_party","$pout_town"),
#    ]),

  (1.5, 0, 0, [(store_random_party_of_template, reg(2), "pt_messenger_party"),
               (party_is_in_any_town,reg(2)),
               ],
   [(store_faction_of_party, ":faction_no", reg(2)),
    (call_script,"script_cf_select_random_center_with_faction", ":faction_no"),
    (party_set_ai_behavior,reg(2),ai_bhvr_travel_to_party),
    (party_set_ai_object,reg(2),reg0),
    (party_set_flags, reg(2), pf_default_behavior, 0),
    ]),
  
  

  (24.0, 0.0, 0.0,
   [
       (ge,"$mountain_bandits_paid",1)
    ],
   [
       (val_sub,"$mountain_bandits_paid",1),
       (eq,"$mountain_bandits_paid",0),
       (set_relation,"fac_mountain_bandits", "fac_player_faction", "$mountain_bandits_relation_original"),
    ]
   ),

  (24.0, 0.0, 0.0,
   [
       (ge,"$forest_bandits_paid",1)
    ],
   [
       (val_sub,"$forest_bandits_paid",1),
       (eq,"$forest_bandits_paid",0),
       (set_relation,"fac_forest_bandits", "fac_player_faction", "$forest_bandits_relation_original"),
    ]
   ),
#Deserters

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_deserters"),
#                         (assign, "$pin_limit", 4),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),
  
#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_deserters"),
#                         (assign, "$pin_limit", 4),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),
  
#  (24.0, 0.0, 0.0,
#   [
#       (ge,"$deserters_paid",1)
#    ],
#   [
#       (val_sub,"$deserters_paid",1),
#       (eq,"$deserters_paid",0),
#       (set_relation,"fac_deserters", "fac_player_faction", "$deserter_relation_original"),
#    ]
#   ),


#Kingdom Parties
  (1.0, 0, 0.0, [],
   [(try_for_range, ":cur_kingdom", kingdoms_begin, kingdoms_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_forager),
##      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_scout),
##      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_patrol),
##      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_messenger),
##      (try_end),
      (try_begin),
        (store_random_in_range, ":random_no", 0, 100),
        (lt, ":random_no", 10),
        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_kingdom_caravan),
      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_prisoner_train),
##      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_deserter),
##      (try_end),
    (try_end),
    ]),


#Swadians
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_swadian_foragers",4)]),
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_swadian_scouts",4)]),
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_swadian_harassers",3)]),
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_swadian_war_parties",2)]),


#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_foragers"),
#                         (assign, "$pin_limit", "$peak_swadian_foragers"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_scouts"),
#                         (assign, "$pin_limit", "$peak_swadian_scouts"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_patrol"),
#                         (assign, "$pin_limit", "$peak_swadian_harassers"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_war_party"),
#                         (assign, "$pin_limit", "$peak_swadian_war_parties"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),
#Vaegirs
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_vaegir_foragers",4)]),
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_vaegir_scouts",4)]),
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_vaegir_harassers",3)]),
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_vaegir_war_parties",2)]),
  

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_foragers"),
#                         (assign, "$pin_limit", "$peak_vaegir_foragers"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_scouts"),
#                         (assign, "$pin_limit", "$peak_vaegir_scouts"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_patrol"),
#                         (assign, "$pin_limit", "$peak_vaegir_harassers"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_war_party"),
#                         (assign, "$pin_limit", "$peak_vaegir_war_parties"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#Villains etc.
#  (14.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_sea_raiders"),
#                         (assign, "$pin_party_template", "pt_sea_raiders"),
#                         (assign, "$pin_limit", 5),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),


#
##  (10.1, 0, 0.0, [],
##                     [
##                         (assign, "$pin_party_template", "pt_refugees"),
##                         (assign, "$pin_limit", 5),
##                         (call_script,"script_cf_spawn_party_at_random_town_if_below_limit"),
##                    ]),
##
##  (10.1, 0, 0.0, [],
##                     [
##                         (assign, "$pin_party_template", "pt_farmers"),
##                         (assign, "$pin_limit", 6),
##                         (call_script,"script_cf_spawn_party_at_random_town_if_below_limit"),
##                    ]),

#  [1.0, 96.0, ti_once, [], [[assign,"$peak_dark_hunters",3]]],
  
##  (10.1, 0, 0.0, [],
##                     [
##                         (assign, "$pin_party_template", "pt_dark_hunters"),
##                         (assign, "$pin_limit", "$peak_dark_hunters"),
##                         (call_script,"script_cf_spawn_party_at_random_town_if_below_limit"),
##                    ]),

#Companion quests

##  (0, 0, ti_once,
##   [
##       (entering_town,"p_town_1"),
##       (main_party_has_troop,"trp_borcha"),
##       (eq,"$borcha_freed",0)
##    ],
##   
##   [
##       (assign,"$borcha_arrive_sargoth_as_prisoner",1),
##       (start_map_conversation,"trp_borcha")
##    ]
##   ),
##
##  (1, 0, ti_once,
##   [
##      (map_free,0),
##      (eq,"$borcha_asked_for_freedom",0),
##      (main_party_has_troop,"trp_borcha")
##    ],
##   [
##       (start_map_conversation,"trp_borcha")
##    ]
##   ),
##  
##  (2, 0, ti_once,
##   [
##      (map_free, 0),
##      (neq,"$borcha_asked_for_freedom",0),
##      (eq,"$borcha_freed",0),
##      (main_party_has_troop,"trp_borcha")
##    ],
##   [
##       (start_map_conversation,"trp_borcha"),
##    ]
##   ),

##### TODO: QUESTS COMMENT OUT BEGIN

###########################################################################
### Random Governer Quest triggers
###########################################################################
##
### Incriminate Loyal Advisor quest
##  (0.2, 0.0, 0.0,
##   [
##       (check_quest_active, "qst_incriminate_loyal_commander"),
##       (quest_slot_eq, "qst_incriminate_loyal_commander", slot_quest_current_state, 2),
##       (quest_get_slot, ":quest_target_center", "qst_incriminate_loyal_commander", slot_quest_target_center),
##       (quest_get_slot, ":quest_target_party", "qst_incriminate_loyal_commander", slot_quest_target_party),
##       (try_begin),
##         (neg|party_is_active, ":quest_target_party"),
##         (quest_set_slot, "qst_incriminate_loyal_commander", slot_quest_current_state, 3),
##         (fail_quest, "qst_incriminate_loyal_commander"),
##       (else_try),
##         (party_is_in_town, ":quest_target_party", ":quest_target_center"),
##         (remove_party, ":quest_target_party"),
##         (quest_set_slot, "qst_incriminate_loyal_commander", slot_quest_current_state, 3),
##         (quest_get_slot, ":quest_object_troop", "qst_incriminate_loyal_commander", slot_quest_object_troop),
##         (call_script, "script_change_troop_faction", ":quest_object_troop", "fac_robber_knights"),
##         (succeed_quest, "qst_incriminate_loyal_commander"),
##       (try_end),
##    ],
##   []
##   ),
### Runaway Peasants quest
##  (0.2, 0.0, 0.0,
##   [
##       (check_quest_active, "qst_bring_back_runaway_serfs"),
##       (neg|check_quest_succeeded, "qst_bring_back_runaway_serfs"),
##       (neg|check_quest_failed, "qst_bring_back_runaway_serfs"),
##       (quest_get_slot, ":quest_object_center", "qst_bring_back_runaway_serfs", slot_quest_object_center),
##       (quest_get_slot, ":quest_target_center", "qst_bring_back_runaway_serfs", slot_quest_target_center),
##       (try_begin),
##         (party_is_active, "$qst_bring_back_runaway_serfs_party_1"),
##         (try_begin),
##           (party_is_in_town, "$qst_bring_back_runaway_serfs_party_1", ":quest_target_center"),
##           (remove_party, "$qst_bring_back_runaway_serfs_party_1"),
##           (val_add, "$qst_bring_back_runaway_serfs_num_parties_fleed", 1),
##         (else_try),
##           (party_is_in_town, "$qst_bring_back_runaway_serfs_party_1", ":quest_object_center"),
##           (remove_party, "$qst_bring_back_runaway_serfs_party_1"),
##           (val_add, "$qst_bring_back_runaway_serfs_num_parties_returned", 1),
##         (else_try),
##           (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_bring_back_runaway_serfs_party_1"),
##           (gt, ":cur_distance", 3),
##           (party_set_ai_object, "$qst_bring_back_runaway_serfs_party_1", ":quest_target_center"),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (party_is_active, "$qst_bring_back_runaway_serfs_party_2"),
##         (try_begin),
##           (party_is_in_town, "$qst_bring_back_runaway_serfs_party_2", ":quest_target_center"),
##           (remove_party, "$qst_bring_back_runaway_serfs_party_2"),
##           (val_add, "$qst_bring_back_runaway_serfs_num_parties_fleed", 1),
##         (else_try),
##           (party_is_in_town, "$qst_bring_back_runaway_serfs_party_2", ":quest_object_center"),
##           (remove_party, "$qst_bring_back_runaway_serfs_party_2"),
##           (val_add, "$qst_bring_back_runaway_serfs_num_parties_returned", 1),
##         (else_try),
##           (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_bring_back_runaway_serfs_party_2"),
##           (gt, ":cur_distance", 3),
##           (party_set_ai_object, "$qst_bring_back_runaway_serfs_party_2", ":quest_target_center"),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (party_is_active, "$qst_bring_back_runaway_serfs_party_3"),
##         (try_begin),
##           (party_is_in_town, "$qst_bring_back_runaway_serfs_party_3", ":quest_target_center"),
##           (remove_party, "$qst_bring_back_runaway_serfs_party_3"),
##           (val_add, "$qst_bring_back_runaway_serfs_num_parties_fleed", 1),
##         (else_try),
##           (party_is_in_town, "$qst_bring_back_runaway_serfs_party_3", ":quest_object_center"),
##           (remove_party, "$qst_bring_back_runaway_serfs_party_3"),
##           (val_add, "$qst_bring_back_runaway_serfs_num_parties_returned", 1),
##         (else_try),
##           (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_bring_back_runaway_serfs_party_2"),
##           (gt, ":cur_distance", 3),
##           (party_set_ai_object, "$qst_bring_back_runaway_serfs_party_2", ":quest_target_center"),
##         (try_end),
##       (try_end),
##       (assign, ":sum_removed", "$qst_bring_back_runaway_serfs_num_parties_returned"),
##       (val_add, ":sum_removed", "$qst_bring_back_runaway_serfs_num_parties_fleed"),
##       (ge, ":sum_removed", 3),
##       (try_begin),
##         (ge, "$qst_bring_back_runaway_serfs_num_parties_returned", 3),
##         (succeed_quest, "qst_bring_back_runaway_serfs"),
##       (else_try),
##         (fail_quest, "qst_bring_back_runaway_serfs"),
##       (try_end),
##    ],
##   []
##   ),
### Defend Nobles Against Peasants quest
##  (0.2, 0.0, 0.0,
##   [
##       (check_quest_active, "qst_defend_nobles_against_peasants"),
##       (neg|check_quest_succeeded, "qst_defend_nobles_against_peasants"),
##       (neg|check_quest_failed, "qst_defend_nobles_against_peasants"),
##       (quest_get_slot, ":quest_target_center", "qst_defend_nobles_against_peasants", slot_quest_target_center),
##       (assign, ":num_active_parties", 0),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_1", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_1"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_1", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_1"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_1"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_2", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_2"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_2", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_2"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_2"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_3", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_3"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_3", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_3"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_3"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_4", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_4"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_4", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_4"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_4"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_5", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_5"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_5", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_5"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_5"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_6", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_6"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_6", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_6"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_6"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_7", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_7"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_7", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_7"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_7"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_8", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_8"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_8", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_8"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_8"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (eq, ":num_active_parties", 0),
##       (try_begin),
##         (store_div, ":limit", "$qst_defend_nobles_against_peasants_num_nobles_to_save", 2),
##         (ge, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":limit"),
##         (succeed_quest, "qst_defend_nobles_against_peasants"),
##       (else_try),
##         (fail_quest, "qst_defend_nobles_against_peasants"),
##       (try_end),
##    ],
##   []
##   ),
### Capture Conspirators quest
##  (0.15, 0.0, 0.0,
##   [
##       (check_quest_active, "qst_capture_conspirators"),
##       (neg|check_quest_succeeded, "qst_capture_conspirators"),
##       (neg|check_quest_failed, "qst_capture_conspirators"),
##       (quest_get_slot, ":quest_target_center", "qst_capture_conspirators", slot_quest_target_center),
##       (quest_get_slot, ":faction_no", "qst_capture_conspirators", slot_quest_target_faction),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_num_parties_to_spawn", "$qst_capture_conspirators_num_parties_spawned"),
##         (store_random_in_range, ":random_no", 0, 100),
##         (lt, ":random_no", 20),
##         (set_spawn_radius, 3),
##         (spawn_around_party,":quest_target_center","pt_conspirator"),
##         (val_add, "$qst_capture_conspirators_num_parties_spawned", 1),
##         (party_get_num_companions, ":num_companions", reg0),
##         (val_add, "$qst_capture_conspirators_num_troops_to_capture", ":num_companions"),
##         (party_set_ai_behavior, reg0, ai_bhvr_travel_to_party),
##         (party_set_ai_object, reg0, "$qst_capture_conspirators_party_1"),
##         (party_set_flags, reg0, pf_default_behavior, 0),
##         (try_begin),
##           (le, "$qst_capture_conspirators_party_2", 0),
##           (assign, "$qst_capture_conspirators_party_2", reg0),
##         (else_try),
##           (le, "$qst_capture_conspirators_party_3", 0),
##           (assign, "$qst_capture_conspirators_party_3", reg0),
##         (else_try),
##           (le, "$qst_capture_conspirators_party_4", 0),
##           (assign, "$qst_capture_conspirators_party_4", reg0),
##         (else_try),
##           (le, "$qst_capture_conspirators_party_5", 0),
##           (assign, "$qst_capture_conspirators_party_5", reg0),
##         (else_try),
##           (le, "$qst_capture_conspirators_party_6", 0),
##           (assign, "$qst_capture_conspirators_party_6", reg0),
##         (else_try),
##           (le, "$qst_capture_conspirators_party_7", 0),
##           (assign, "$qst_capture_conspirators_party_7", reg0),
##         (try_end),
##       (try_end),
##
##       (assign, ":num_active_parties", 0),
##
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_1", 0),
##         (party_is_active, "$qst_capture_conspirators_party_1"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_1"),
##           (remove_party, "$qst_capture_conspirators_party_1"),
##         (else_try),
##           (party_get_num_attached_parties, ":num_attachments", "$qst_capture_conspirators_party_1"),
##           (gt, ":num_attachments", 0),
##           (assign, ":leave_meeting", 0),
##           (try_begin),
##             (store_sub, ":required_attachments", "$qst_capture_conspirators_num_parties_to_spawn", 1),
##             (eq, ":num_attachments", ":required_attachments"),
##             (val_add, "$qst_capture_conspirators_leave_meeting_counter", 1),
##             (ge, "$qst_capture_conspirators_leave_meeting_counter", 15),
##             (assign, ":leave_meeting", 1),
##           (try_end),
##           (try_begin),
##             (eq, "$qst_capture_conspirators_num_parties_to_spawn", "$qst_capture_conspirators_num_parties_spawned"),
##             (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_capture_conspirators_party_1"),
##             (assign, ":min_distance", 3),
##             (try_begin),
##               (is_currently_night),
##               (assign, ":min_distance", 2),
##             (try_end),
##             (lt, ":cur_distance", ":min_distance"),
##             (assign, "$qst_capture_conspirators_leave_meeting_counter", 15),
##             (assign, ":leave_meeting", 1),
##           (try_end),
##           (eq, ":leave_meeting", 1),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_1", ai_bhvr_travel_to_point),
##           (party_set_flags, "$qst_capture_conspirators_party_1", pf_default_behavior, 0),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_1"),
##           (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##           (party_set_ai_target_position, "$qst_capture_conspirators_party_1", pos2),
##           (try_begin),
##             (gt, "$qst_capture_conspirators_party_2", 0),
##             (party_detach, "$qst_capture_conspirators_party_2"),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_2", ai_bhvr_travel_to_point),
##             (party_set_flags, "$qst_capture_conspirators_party_2", pf_default_behavior, 0),
##             (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##             (party_set_ai_target_position, "$qst_capture_conspirators_party_2", pos2),
##           (try_end),
##           (try_begin),
##             (gt, "$qst_capture_conspirators_party_3", 0),
##             (party_detach, "$qst_capture_conspirators_party_3"),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_3", ai_bhvr_travel_to_point),
##             (party_set_flags, "$qst_capture_conspirators_party_3", pf_default_behavior, 0),
##             (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##             (party_set_ai_target_position, "$qst_capture_conspirators_party_3", pos2),
##           (try_end),
##           (try_begin),
##             (gt, "$qst_capture_conspirators_party_4", 0),
##             (party_detach, "$qst_capture_conspirators_party_4"),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_4", ai_bhvr_travel_to_point),
##             (party_set_flags, "$qst_capture_conspirators_party_4", pf_default_behavior, 0),
##             (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##             (party_set_ai_target_position, "$qst_capture_conspirators_party_4", pos2),
##           (try_end),
##           (try_begin),
##             (gt, "$qst_capture_conspirators_party_5", 0),
##             (party_detach, "$qst_capture_conspirators_party_5"),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_5", ai_bhvr_travel_to_point),
##             (party_set_flags, "$qst_capture_conspirators_party_5", pf_default_behavior, 0),
##             (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##             (party_set_ai_target_position, "$qst_capture_conspirators_party_5", pos2),
##           (try_end),
##           (try_begin),
##             (gt, "$qst_capture_conspirators_party_6", 0),
##             (party_detach, "$qst_capture_conspirators_party_6"),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_6", ai_bhvr_travel_to_point),
##             (party_set_flags, "$qst_capture_conspirators_party_6", pf_default_behavior, 0),
##             (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##             (party_set_ai_target_position, "$qst_capture_conspirators_party_6", pos2),
##           (try_end),
##           (try_begin),
##             (gt, "$qst_capture_conspirators_party_7", 0),
##             (party_detach, "$qst_capture_conspirators_party_7"),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_7", ai_bhvr_travel_to_point),
##             (party_set_flags, "$qst_capture_conspirators_party_7", pf_default_behavior, 0),
##             (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##             (party_set_ai_target_position, "$qst_capture_conspirators_party_7", pos2),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_1"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_1"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_1"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_1", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_1", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_1", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_1", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_2", 0),
##         (party_is_active, "$qst_capture_conspirators_party_2"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_2"),
##           (try_begin),
##             (neg|party_is_in_town, "$qst_capture_conspirators_party_2", "$qst_capture_conspirators_party_1"),
##             (remove_party, "$qst_capture_conspirators_party_2"),
##           (else_try),
##             (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_2"),
##             (neq, ":ai_behavior", ai_bhvr_hold),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_2", ai_bhvr_hold),
##             (party_attach_to_party, "$qst_capture_conspirators_party_2", "$qst_capture_conspirators_party_1"),
##             (party_set_flags, "$qst_capture_conspirators_party_2", pf_default_behavior, 0),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_2"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_2"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_2"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_2", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_2", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_2", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_2", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_3", 0),
##         (party_is_active, "$qst_capture_conspirators_party_3"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_3"),
##           (try_begin),
##             (neg|party_is_in_town, "$qst_capture_conspirators_party_3", "$qst_capture_conspirators_party_1"),
##             (remove_party, "$qst_capture_conspirators_party_3"),
##           (else_try),
##             (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_3"),
##             (neq, ":ai_behavior", ai_bhvr_hold),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_3", ai_bhvr_hold),
##             (party_attach_to_party, "$qst_capture_conspirators_party_3", "$qst_capture_conspirators_party_1"),
##             (party_set_flags, "$qst_capture_conspirators_party_3", pf_default_behavior, 0),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_3"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_3"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_3"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_3", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_3", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_3", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_3", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_4", 0),
##         (party_is_active, "$qst_capture_conspirators_party_4"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_4"),
##           (try_begin),
##             (neg|party_is_in_town, "$qst_capture_conspirators_party_4", "$qst_capture_conspirators_party_1"),
##             (remove_party, "$qst_capture_conspirators_party_4"),
##           (else_try),
##             (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_4"),
##             (neq, ":ai_behavior", ai_bhvr_hold),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_4", ai_bhvr_hold),
##             (party_set_flags, "$qst_capture_conspirators_party_4", pf_default_behavior, 0),
##             (party_attach_to_party, "$qst_capture_conspirators_party_4", "$qst_capture_conspirators_party_1"),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_4"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_4"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_4"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_4", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_4", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_4", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_4", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_5", 0),
##         (party_is_active, "$qst_capture_conspirators_party_5"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_5"),
##           (try_begin),
##             (neg|party_is_in_town, "$qst_capture_conspirators_party_5", "$qst_capture_conspirators_party_1"),
##             (remove_party, "$qst_capture_conspirators_party_5"),
##           (else_try),
##             (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_5"),
##             (neq, ":ai_behavior", ai_bhvr_hold),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_5", ai_bhvr_hold),
##             (party_set_flags, "$qst_capture_conspirators_party_5", pf_default_behavior, 0),
##             (party_attach_to_party, "$qst_capture_conspirators_party_5", "$qst_capture_conspirators_party_1"),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_5"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_5"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_5"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_5", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_5", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_5", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_5", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_6", 0),
##         (party_is_active, "$qst_capture_conspirators_party_6"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_6"),
##           (try_begin),
##             (neg|party_is_in_town, "$qst_capture_conspirators_party_6", "$qst_capture_conspirators_party_1"),
##             (remove_party, "$qst_capture_conspirators_party_6"),
##           (else_try),
##             (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_6"),
##             (neq, ":ai_behavior", ai_bhvr_hold),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_6", ai_bhvr_hold),
##             (party_set_flags, "$qst_capture_conspirators_party_6", pf_default_behavior, 0),
##             (party_attach_to_party, "$qst_capture_conspirators_party_6", "$qst_capture_conspirators_party_1"),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_6"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_6"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_6"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_6", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_6", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_6", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_6", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_7", 0),
##         (party_is_active, "$qst_capture_conspirators_party_7"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_7"),
##           (try_begin),
##             (neg|party_is_in_town, "$qst_capture_conspirators_party_7", "$qst_capture_conspirators_party_1"),
##             (remove_party, "$qst_capture_conspirators_party_7"),
##           (else_try),
##             (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_7"),
##             (neq, ":ai_behavior", ai_bhvr_hold),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_7", ai_bhvr_hold),
##             (party_set_flags, "$qst_capture_conspirators_party_7", pf_default_behavior, 0),
##             (party_attach_to_party, "$qst_capture_conspirators_party_7", "$qst_capture_conspirators_party_1"),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_7"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_7"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_7"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_7", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_7", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_7", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_7", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##
##       (eq, ":num_active_parties", 0),
##       (party_count_prisoners_of_type, ":count_captured_conspirators", "p_main_party", "trp_conspirator"),
##       (party_count_prisoners_of_type, ":count_captured_conspirator_leaders", "p_main_party", "trp_conspirator_leader"),
##       (val_add, ":count_captured_conspirators", ":count_captured_conspirator_leaders"),
##       (try_begin),
##         (store_div, ":limit", "$qst_capture_conspirators_num_troops_to_capture", 2),
##         (gt, ":count_captured_conspirators", ":limit"),
##         (succeed_quest, "qst_capture_conspirators"),
##       (else_try),
##         (fail_quest, "qst_capture_conspirators"),
##       (try_end),
##    ],
##   []
##   ),
### Follow Spy quest
##  (0.5, 0.0, 0.0,
##   [
##       (check_quest_active, "qst_follow_spy"),
##       (neg|check_quest_succeeded, "qst_follow_spy"),
##       (neg|check_quest_failed, "qst_follow_spy"),
##       (quest_get_slot, ":quest_giver_center", "qst_follow_spy", slot_quest_giver_center),
##       (quest_get_slot, ":quest_object_center", "qst_follow_spy", slot_quest_object_center),
##       (assign, ":abort_meeting", 0),
##       (try_begin),
##         (this_or_next|eq, "$qst_follow_spy_run_away", 1),
##         (this_or_next|neg|party_is_active, "$qst_follow_spy_spy_party"),
##         (neg|party_is_active, "$qst_follow_spy_spy_partners_party"),
##       (else_try),
##         (eq, "$qst_follow_spy_meeting_state", 0),
##         (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_follow_spy_spy_party"),
##         (try_begin),
##           (assign, ":min_distance", 3),
##           (try_begin),
##             (is_currently_night),
##             (assign, ":min_distance", 1),
##           (try_end),
##           (le, ":cur_distance", ":min_distance"),
##           (store_distance_to_party_from_party, ":player_distance_to_quest_giver_center", "p_main_party", ":quest_giver_center"),
##           (gt, ":player_distance_to_quest_giver_center", 1),
##           (assign, ":abort_meeting", 1),
##           (assign, "$qst_follow_spy_run_away", 1),
##           (display_message, "str_qst_follow_spy_noticed_you"),
##         (else_try),
##           (store_distance_to_party_from_party, ":cur_distance", "$qst_follow_spy_spy_partners_party", "$qst_follow_spy_spy_party"),
##           (le, ":cur_distance", 1),
##           (party_attach_to_party, "$qst_follow_spy_spy_party", "$qst_follow_spy_spy_partners_party"),
##           (assign, "$qst_follow_spy_meeting_state", 1),
##           (assign, "$qst_follow_spy_meeting_counter", 0),
##         (try_end),
##       (else_try),
##         (eq, "$qst_follow_spy_meeting_state", 1),
##         (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_follow_spy_spy_partners_party"),
##         (try_begin),
##           (le, ":cur_distance", 1),
##           (party_detach, "$qst_follow_spy_spy_party"),
##           (assign, ":abort_meeting", 1),
##           (assign, "$qst_follow_spy_run_away", 1),
##           (display_message, "str_qst_follow_spy_noticed_you"),
##         (else_try),
##           (val_add, "$qst_follow_spy_meeting_counter", 1),
##           (gt, "$qst_follow_spy_meeting_counter", 4),
##           (party_detach, "$qst_follow_spy_spy_party"),
##           (assign, ":abort_meeting", 0),
##           (assign, "$qst_follow_spy_meeting_state", 2),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (eq, ":abort_meeting", 1),
##         (party_set_ai_object, "$qst_follow_spy_spy_party", ":quest_giver_center"),
##         (party_set_ai_object, "$qst_follow_spy_spy_partners_party", ":quest_object_center"),
##         (party_set_ai_behavior, "$qst_follow_spy_spy_party", ai_bhvr_travel_to_party),
##         (party_set_ai_behavior, "$qst_follow_spy_spy_partners_party", ai_bhvr_travel_to_party),
##         (party_set_flags, "$qst_follow_spy_spy_party", pf_default_behavior, 0),
##         (party_set_flags, "$qst_follow_spy_spy_partners_party", pf_default_behavior, 0),
##       (try_end),
##       (try_begin),
##         (party_is_active, "$qst_follow_spy_spy_party"),
##         (party_is_in_town, "$qst_follow_spy_spy_party", ":quest_giver_center"),
##         (remove_party, "$qst_follow_spy_spy_party"),
##         (assign, "$qst_follow_spy_spy_back_in_town",1),
##       (try_end),
##       (try_begin),
##         (party_is_active, "$qst_follow_spy_spy_partners_party"),
##         (party_is_in_town, "$qst_follow_spy_spy_partners_party", ":quest_object_center"),
##         (remove_party, "$qst_follow_spy_spy_partners_party"),
##         (assign, "$qst_follow_spy_partner_back_in_town",1),
##       (try_end),
##       (try_begin),
##         (eq, "$qst_follow_spy_partner_back_in_town",1),
##         (eq, "$qst_follow_spy_spy_back_in_town",1),
##         (fail_quest, "qst_follow_spy"),
##       (try_end),
##    ],
##   []
##   ),
### Raiders quest
##  (0.95, 0.0, 0.2,
##   [
##       (check_quest_active, "qst_hunt_down_raiders"),
##       (neg|check_quest_succeeded, "qst_hunt_down_raiders"),
##       (neg|check_quest_failed, "qst_hunt_down_raiders"),
##    ],
##   [
##       (quest_get_slot, ":quest_target_party", "qst_hunt_down_raiders", slot_quest_target_party),
##       (party_set_ai_behavior, ":quest_target_party", ai_bhvr_hold),
##       (party_set_flags, ":quest_target_party", pf_default_behavior, 0),
##    ]
##   ),
##
##  (0.7, 0, 0.2,
##   [
##       (check_quest_active, "qst_hunt_down_raiders"),
##       (neg|check_quest_succeeded, "qst_hunt_down_raiders"),
##       (neg|check_quest_failed, "qst_hunt_down_raiders"),
##    ],
##   [
##       (quest_get_slot, ":quest_target_party", "qst_hunt_down_raiders", slot_quest_target_party),
##       (party_set_ai_behavior,":quest_target_party",ai_bhvr_travel_to_party),
##       (party_set_flags, ":quest_target_party", pf_default_behavior, 0),
##    ]
##   ),
##  
##  (0.1, 0.0, 0.0,
##   [
##       (check_quest_active, "qst_hunt_down_raiders"),
##       (neg|check_quest_succeeded, "qst_hunt_down_raiders"),
##       (neg|check_quest_failed, "qst_hunt_down_raiders"),
##       (quest_get_slot, ":quest_target_party", "qst_hunt_down_raiders", slot_quest_target_party),
##       (neg|party_is_active, ":quest_target_party")
##    ],
##   [
##       (succeed_quest, "qst_hunt_down_raiders"),
##    ]
##   ),
##  
##  (1.3, 0, 0.0,
##   [
##       (check_quest_active, "qst_hunt_down_raiders"),
##       (neg|check_quest_succeeded, "qst_hunt_down_raiders"),
##       (neg|check_quest_failed, "qst_hunt_down_raiders"),
##       (quest_get_slot, ":quest_target_party", "qst_hunt_down_raiders", slot_quest_target_party),
##       (quest_get_slot, ":quest_target_center", "qst_hunt_down_raiders", slot_quest_target_center),
##       (party_is_in_town,":quest_target_party",":quest_target_center")
##    ],
##   [
##       (fail_quest, "qst_hunt_down_raiders"),
##       (display_message, "str_raiders_reached_base"),
##       (quest_get_slot, ":quest_target_party", "qst_hunt_down_raiders", slot_quest_target_party),
##       (remove_party, ":quest_target_party"),
##    ]
##   ),

##### TODO: QUESTS COMMENT OUT END

#########################################################################
# Random MERCHANT quest triggers
####################################  
 # Apply interest to merchants guild debt  1% per week
  (24.0 * 7, 0.0, 0.0,
   [],
   [
       (val_mul,"$debt_to_merchants_guild",101),
       (val_div,"$debt_to_merchants_guild",100)
    ]
   ),
# Deliver food to tavern  
  (24.0, 0, 1.0,
   [
       (eq,"$deliver_wine_quest_active",1),
       (store_current_day,reg(3)),
       (lt,"$deliver_wine_quest_end_day",reg(3)),
     ],
   [
       (fail_quest,"qst_deliver_wine"),
       (call_script, "script_end_quest","qst_deliver_wine"),
       (assign,"$deliver_wine_quest_active",0),
       (val_add,"$debt_to_merchants_guild","$deliver_wine_quest_debt")
     ]
   ),
# Escort merchant caravan:
  (0.1, 0.0, 0.1, [[eq,"$escort_merchant_caravan_quest_active",1],
                   [eq,"$escort_merchant_caravan_mode",1]], [[party_set_ai_behavior,"$merchant_caravan_id",ai_bhvr_hold],[party_set_flags, "$merchant_caravan_id", pf_default_behavior, 0]]),
  (0.02, 0.0, 0.1, [[eq,"$escort_merchant_caravan_quest_active",1],
                   [eq,"$escort_merchant_caravan_mode",0],
#                   [store_distance_to_party_from_party, reg(0),"$merchant_caravan_id"],
#                   [ge,reg(0),1]
                   ], [[party_set_ai_behavior,"$merchant_caravan_id",ai_bhvr_escort_party],[party_set_flags, "$merchant_caravan_id", pf_default_behavior, 0],
                                    [party_set_ai_object,"$merchant_caravan_id",0]]),
  (0.02, 0.0, 0.1, [[eq,"$escort_merchant_caravan_quest_active",1],
                   [eq,"$escort_merchant_caravan_mode",0],
                   [store_distance_to_party_from_party, reg(0),"$merchant_caravan_id"],
                   [lt,reg(0),1]], [
#                   [party_set_ai_behavior,"$merchant_caravan_id",ai_bhvr_hold],[party_set_flags, "$merchant_caravan_id", pf_default_behavior, 0]
                   ]),

  (0.1, 0, 0.0, [[eq,"$escort_merchant_caravan_quest_active",1],
                 [neg|party_is_active,"$merchant_caravan_id"]],
                         [[fail_quest,"qst_escort_merchant_caravan"],
                          [call_script, "script_end_quest","qst_escort_merchant_caravan"],
                           [assign,"$escort_merchant_caravan_quest_active",0]]),

# Troublesome bandits
  (0.3, 0.0, 1.1, [[eq,"$troublesome_bandits_quest_active",1],
                   [store_num_parties_destroyed,reg(3),"pt_troublesome_bandits"],
                   [lt,"$troublesome_bandits_eliminated",reg(3)],
                   (store_num_parties_destroyed_by_player, ":num_quest_bandits_destroyed_by_player", "pt_troublesome_bandits"),
                   [eq,":num_quest_bandits_destroyed_by_player","$troublesome_bandits_eliminated_by_player"],
                   ], [[display_message,"str_bandits_eliminated_by_another"],
                       [fail_quest,"qst_troublesome_bandits"],
                       [call_script, "script_end_quest","qst_troublesome_bandits"],
                       [assign,"$troublesome_bandits_quest_active",0]]),

  (0.3, 0.0, 1.1, [[eq,"$troublesome_bandits_quest_active",1],
                   [store_num_parties_destroyed,reg(3),"pt_troublesome_bandits"],
                   [lt,"$troublesome_bandits_eliminated",reg(3)],
                   (store_num_parties_destroyed_by_player, ":num_quest_bandits_destroyed_by_player", "pt_troublesome_bandits"),
                   [neq,":num_quest_bandits_destroyed_by_player","$troublesome_bandits_eliminated_by_player"],
                   ], [[succeed_quest,"qst_troublesome_bandits"],
                       [assign,"$troublesome_bandits_quest_active",0],
                       [assign,"$troublesome_bandits_quest_succeeded",1]]),
# Kidnapped girl:
  (0, 0, 0,
   [
       (entering_town,"$kidnapped_girl_quest_giver_town"),
       (main_party_has_troop,"trp_kidnapped_girl")
       ],
   [
       (start_map_conversation,"trp_kidnapped_girl"),
       (assign,"$talking_with_kidnapped_girl",1),
       (assign,"$kidnapped_girl_brought_back",1)
       ]
   ),
  
  (2.0, 0, 0,
   [
      (check_quest_active,"qst_kidnapped_girl"),
      (main_party_has_troop,"trp_kidnapped_girl"),
      (eq,"$kidnapped_girl_bandits_attacked",1),
      (eq,"$kidnapped_girl_joined",0),
    ],
   [
      (assign,"$kidnapped_girl_joined",1),
    ]
   ),
 
]
